package org.springframework.prospring.ticket.service.impl;

import org.springframework.prospring.ticket.service.ClubManager;
import org.springframework.prospring.ticket.dao.ClubDAO;
import org.springframework.prospring.ticket.domain.Club;
import org.springframework.prospring.ticket.domain.Field;
import org.springframework.prospring.ticket.domain.Turn;
import org.springframework.prospring.ticket.domain.user.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Collection;
import java.util.Date;
import java.sql.Timestamp;

public class ClubManagerImpl implements ClubManager {
    private static Log logger = LogFactory.getLog(ClubManagerImpl.class);
    private ClubDAO dao;

    public ClubManagerImpl() {
        super();
        if (logger.isDebugEnabled()) {
            logger.debug("in ClubManagerImpl Constructor");
        }
    }

    public void setClubDAO(ClubDAO dao) {
        if (logger.isDebugEnabled()) {
            logger.debug("setting ClubDAO: " + dao);
        }

        this.dao = dao;
    }

    public Club findClub(Long clubId) {
        if (logger.isDebugEnabled()) {
            logger.debug("finding Club id: "+clubId);
        }
        Club club = dao.findClub(clubId);

        if (club == null) {
            logger.warn("ClubId '" + clubId + "' not found in database.");
        }

        return club;
    }

    public Collection<Club> findAllClubs(){
        if (logger.isDebugEnabled()) {
            logger.debug("finding all Clubs ");
        }

        Collection<Club> clubs = dao.findAllClubs();

        if (clubs == null) {
            logger.warn("Not found any Clubs in database.");
        }

        return clubs;
    }

    public Collection<Club> findAllClubsByRegionId(Long regionId){
        if (logger.isDebugEnabled()) {
            logger.debug("finding all Clubs by region id="+regionId);
        }

        Collection<Club> clubs = dao.findAllClubsByRegionId(regionId);

        if (clubs == null) {
            logger.warn("Not found any Clubs in database for region id="+regionId);
        }

        return clubs;
    }

    public Collection<Field> findFields(Long clubId){
        if (logger.isDebugEnabled()) {
            logger.debug("finding all Field by club id="+clubId);
        }

        Collection<Field> fields = dao.findFields(clubId);

        if (fields == null) {
            logger.warn("Not found any Fields in database for clubs id="+clubId);
        }

        return fields;
    }

    public Collection<Turn> findAllTurnsByFieldIdAndDate(Long fieldId, Date date){
        if (logger.isDebugEnabled()) {
            logger.debug("finding all Turns by field id=" + fieldId + " and date=" + date);
        }
        Collection<Turn> turns = dao.findAllTurnsByFieldIdAndDate(fieldId, date);

        if (turns == null) {
            logger.warn("Not found any Turns in database for field id=" + fieldId + " and date="+date);
        }

        return turns;
    }

    public Turn saveReservationField(Long fieldId, Timestamp startDate, Timestamp endDate, User user){
        if (logger.isDebugEnabled()) {
            logger.debug("making Reservation on field id=" + fieldId + " with startDate=" + startDate + ", endDate="+endDate + ", user="+user);
        }

        return dao.saveReservationField(fieldId, startDate, endDate, user);
    }

    public Club saveClub(Club club) throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("saving Club: " + club);
        }

        dao.saveClub(club);

        return club;
    }

    public void removeClub(Long clubId) {
        dao.removeClub(clubId);
    }


}
